package com.zx.baofu.alcohol.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class RedisUtil {
    @Autowired
    private JedisPool pool;

    public String set(String key, String s, int seconds){
        if (null == key || null == s){
            return "";
        }
        Jedis jedis = null;
        String res = "";
        try{
            jedis = pool.getResource();
            res = jedis.set(key, s);
            jedis.expire(key, seconds);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null){
                jedis.close();
            }
            return res;
        }
    }

    public String get(String key){
        if (null == key){
            return "";
        }
        Jedis jedis = null;
        String res = "";
        try{
            jedis = pool.getResource();
            res = jedis.get(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null){
                jedis.close();
            }
            return res;
        }
    }

    public Long del(String key){
        if (null == key){
            return 0L;
        }
        Jedis jedis = null;
        Long res = 0L;
        try{
            jedis = pool.getResource();
            res = jedis.del(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null){
                jedis.close();
            }
            return res;
        }
    }
}
